#include<list>
#include<iterator>
#include<iostream>
using namespace std;

class BinaryAdd : public list<int>
{
public:
    BinaryAdd(int value);
    void AddByOne(void);
    friend ostream& operator<<(ostream& os, BinaryAdd const& b);
};

BinaryAdd::BinaryAdd(int value)
{
    while (0 != value)
    {
        push_front(value % 2);
        value /= 2;       
    }
}

ostream& operator<<(ostream& os, BinaryAdd const& b)
{
    list<int>::const_iterator it, itEnd = b.end();
    int sum = 0;
    
    for (it = b.begin(); it != itEnd; ++it)
    {
        sum <<= 1;
        sum += *it;
    }
    
    os<< sum <<endl;
    
    return os;
}

void BinaryAdd::AddByOne(void)
{
    list<int>::reverse_iterator it = find(rbegin(), rend(), 0);
    fill(rbegin(), it, 0);
    
    if (it != rend())
    {
        *it = 1;
    }
    else
    {
        push_front(1);
    }
}

int main(void)
{
    BinaryAdd bOne(23);
    bOne.AddByOne();
    cout<< bOne;
    
    BinaryAdd bTwo(15);
    bTwo.AddByOne();
    cout<< bTwo;
    
    
    system("pause");
}
